package com.ksd.api.controller;

import com.ksd.api.beans.*;
import com.ksd.api.utils.HttpServletUtil;
import com.ksd.api.beans.WeatherBean;
import com.ksd.common.core.controller.BaseController;
import com.ksd.common.core.domain.AjaxResult;
import com.ksd.common.utils.StringUtils;
import com.ksd.system.domain.SysBoard;
import com.ksd.system.domain.SysFirst;
import com.ksd.system.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class GetFirstPageConfig extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(GetFirstPageConfig.class);
    @Autowired
    private ISysFirstService sysFirstService;
    @Autowired
    private ISysBoardService sysBoardService;

    @RequestMapping(value = "/api/user/apk/getFirstPageConfig", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult getPageConfig(HttpServletRequest httpServletRequest) {
        logger.info("===================获取APK首页配置信息===============");

        HttpServletUtil.getRequestParam(httpServletRequest);

        SysBoard sysBoard = sysBoardService.selectSysBoardById(SysBoard.serviceFileId);
        String serviceUrl = sysBoard.getUrl();

        FirstPageResponse firstPageResponse = new FirstPageResponse();
        SysFirst sysFirstWeater = sysFirstService.selectSysFirstById(SysFirst.weather);
        WeatherBean weatherBean = new WeatherBean();
        weatherBean.setCity(sysFirstWeater.getValue1());
        if (StringUtils.isNotEmpty(sysFirstWeater.getValue2())) {
            weatherBean.setRegion(sysFirstWeater.getValue2());
        }
        weatherBean.setWeatherMessage(sysFirstWeater.getValue3());
        firstPageResponse.setWeatherBean(weatherBean);

        SysFirst sysFirstIntro = sysFirstService.selectSysFirstById(SysFirst.introduce_message);
        IntroduceBean introduceBean = new IntroduceBean();
        introduceBean.setMessageDetail(sysFirstIntro.getDescription());
        introduceBean.setWeatherDetail(sysFirstWeater.getValue4());
        firstPageResponse.setIntroduceBean(introduceBean);

        SysFirst sysFirstTopLeft = sysFirstService.selectSysFirstById(SysFirst.top_left);
        SysFirst sysFirstTopRight = sysFirstService.selectSysFirstById(SysFirst.top_right);
        SysFirst sysFirstLowerLeft = sysFirstService.selectSysFirstById(SysFirst.lower_left);
        SysFirst sysFirstLowerRight = sysFirstService.selectSysFirstById(SysFirst.lower_right);
        SysFirst sysFirstPageColor = sysFirstService.selectSysFirstById(SysFirst.page_color_url);
        SysFirst sysFirstPageHotelVideo = sysFirstService.selectSysFirstById(SysFirst.hotel_video_introduction_url);
        FirstMainBean firstMainBean = new FirstMainBean();
        firstMainBean.setTopLeftPicUrl(serviceUrl+sysFirstTopLeft.getValue1());
        firstMainBean.setTopLeftVideoUrl(serviceUrl+sysFirstTopLeft.getValue2());
        firstMainBean.setTopRightPicUrl(serviceUrl+sysFirstTopRight.getValue1());
        firstMainBean.setTopRightVideoUrl(serviceUrl+sysFirstTopRight.getValue2());
        firstMainBean.setLowerLeftPicUrl(serviceUrl+sysFirstLowerLeft.getValue1());
        firstMainBean.setLowerLeftVideoUrl(serviceUrl+sysFirstLowerLeft.getValue2());
        firstMainBean.setLowerRightPicUrl(serviceUrl+sysFirstLowerRight.getValue1());
        firstMainBean.setLowerRightVideoUrl(serviceUrl+sysFirstLowerRight.getValue2());
        firstPageResponse.setFirstMainBean(firstMainBean);
        firstPageResponse.setHotelVideoUrl(serviceUrl+sysFirstPageHotelVideo.getValue1());
        //background: linear-gradient(to bottom, rgb(34, 28, 28), rgb(69, 59, 58));
        //background-image: url(./static/app6.png);
        // background-position: 0% 0%;
        // background-size: 100% 100%;
        // background-repeat: no-repeat;
        String value2 = sysFirstPageColor.getValue2();

        if (value2.equals("1")){
            logger.info("背景图:"+sysFirstPageColor.getValue1());
            String img = "background-image: url(\""+serviceUrl+sysFirstPageColor.getValue1()+"\");background-position: 0% 0%;background-size: 100% 100%;background-repeat: no-repeat;";
            logger.info("img:"+img);
            firstPageResponse.setFirstPageUrlOrColor(img);
        }else if (value2.equals("0")){
            String color = "background: "+sysFirstPageColor.getValue3();
            firstPageResponse.setFirstPageUrlOrColor(color);
        }
        logger.info(firstPageResponse.toString());
        return AjaxResult.success(firstPageResponse);
    }

}